package com.lubase.metadata.pageextend;

import com.lubase.core.extend.PageDataExtendService;
import com.lubase.model.DbEntity;
import com.lubase.model.DbField;
import com.lubase.model.DbTable;
import com.lubase.model.DbCollection;
import com.lubase.orm.model.LoginUser;
import com.lubase.orm.service.AppHolderService;
import com.lubase.orm.util.TypeConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatasourceManagePageExtend implements PageDataExtendService {
    @Override
    public String getPageId() {
        return "1472522436585984000";
    }

    @Override
    public String getDescription() {
        return "后台计算编辑按钮显示权限";
    }

    @Autowired
    AppHolderService appHolderService;

    @Override
    public DbCollection beforeReturnMainData(String pageId, DbCollection collection) {
        DbTable table = collection.getTableInfo();
        String colCode = "_setting";
        DbField field = new DbField();
        field.setId(colCode);
        field.setCode(field.getId());
        field.setName("配置权限");
        field.setVisible(0);
        field.setEleType("1");
        table.getFieldList().add(field);

        LoginUser loginUser = appHolderService.getUser();
        for (DbEntity entity : collection.getData()) {
            String dbManager = TypeConverterUtils.object2String(entity.get("db_manager"), "");
            entity.put("_setting", dbManager.contains(loginUser.getCode()) ? 1 : 0);
        }
        return collection;
    }
}
